package TUILayer;//makes this class a part of the TUILayer package
import java.util.Scanner;

/**
 * 
 * User interface - main menu
 * The entry-point for the application:
 * Create an instance of class MainMenuUI and the constructor starts the application
 * 
 * @author Gruppe A
 * @version 1.0
 */
public class MainMenuUI
{
  //the reference to the ControlLayer
   private AddressBookUI adrUI;//references to other UI objects
   private DvdUI dvdUI;
   private LoanUI loanUI;
   
   /**
    * Constructor for our main entry point
    */
   public MainMenuUI()
   {
  
      
   }
   
   /**
    * Our main method for executing commands and methods for the entire application. It has some redudancy built into it that prevents the user from using invalid numbers.
    */
   public void mainMenuStart()
   {
        boolean exit=false;
        while (!exit) //! means while exit not is true (that is: false)
        {
            int choice = writeMainMenu();
            if(choice != 1 && choice != 2 && choice != 3 && choice != 4)
            {
                System.out.println("You have to choose either 1, 2, 3 or 4");
                
            }
            if(choice == 1)
            {
                startAddressBook();
            }
            else{
                    if(choice == 2){ 
                        startDvd();
                    }
                    else{
                        if(choice == 3){
                        startLoan();
                            }//end if
                        else{
                            if(choice == 4)
                            {
                            writeEnd();
                            exit = true;}
                        }//end else
                    }// end else
             }//end else
        }//end while
    }//end start
    
    /**
     * Our main input method it both receives the input from the user aswell as creates the actual menu from where the user makes his or her choices, it has redundancy built into it, so that you can't
     * use incorect input methods.
     */
   private int writeMainMenu()
    {       
            // creates a keyboard object to read input
            Scanner keyboard = new Scanner(System.in);
            System.out.println(" *** Main Menu ***");
            System.out.println(" (1) Address Book");
            System.out.println(" (2) DVDs");
            System.out.println(" (3) Loan");
            System.out.println(" (4) Close");
            System.out.print("\n Make your choice: ");
            
            boolean ok = false;
            int choice = 0;
            while(!ok)
            {
            try{
                choice = keyboard.nextInt();
                ok = true;
            }
            catch (Exception e)
            {
                System.out.println("You must enter a number - try again");
                String input = keyboard.nextLine();
            }
        }//end while
            
            
            return choice;
    }
    
    /**
     * The method used for starting our addressbook UI
     */
    private void startAddressBook()
    {
        adrUI= new AddressBookUI();
        adrUI.start();
      
     
    }

    /**
     * The method used for starting our dvd UI
     */
    private void startDvd()
    {
        dvdUI= new DvdUI();
        dvdUI.start();

     
    }
    
    /**
     * The method used for starting our loan UI
     */
    private void startLoan()
    {
       loanUI = new LoanUI();
       loanUI.start();
    }
    
    /**
     * The method used for ending the program. It prints a nice little confirmation message.
     */
    private void writeEnd()
    {
        System.out.println("The System is shutting down ");
    }
 }
